import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/widgets/choice_bar.dart';
import 'package:ball_lion/module/record/channel_logic.dart';
import 'package:ball_lion/entity/record/channel_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class ChannelPage extends StatefulWidget {
  const ChannelPage({super.key, this.data, this.type});

  final ChannelEntity? data;
  final int? type;

  @override
  State<ChannelPage> createState() => _ChannelPageState();
}

class _ChannelPageState extends State<ChannelPage> {
  late ChannelLogic logic = Get.put(ChannelLogic(),
      tag: "${widget.data?.channelId}${widget.type ?? ""}");

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChannelLogic>(
        tag: "${widget.data?.channelId}${widget.type ?? ""}",
        initState: (state) {
          logic.recordType = widget.type ?? 0;
          logic.tag = "${widget.data?.channelId}${widget.type ?? ""}";
          logic.getData(widget.data);
        },
        builder: (logic) {
          return SizedBox(
            height: Get.height,
            child: Column(children: [
              pageChoice(),
              Expanded(
                  child: PageView(
                physics: const NeverScrollableScrollPhysics(),
                controller: logic.pageController,
                onPageChanged: (index) => logic.typeIndex = index,
                children: logic.page,
              ))
            ]),
          );
        });
  }

  Widget pageChoice() {
    return Container(
      decoration: const BoxDecoration(
          border: Border(bottom: BorderSide(width: 0.5, color: Colours.N3))),
      padding: const EdgeInsets.only(left: 12, right: 12, top: 6, bottom: 6),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          GestureDetector(
            onTap: () {
              Utils.onEvent("sjpd_lx", params: {"sjpd_lx": 0});
              logic.showDatePicker();
            },
            child: Container(
              // key: ValueKey(widget.data?.channelId),
              height: 24,
              alignment: Alignment.center,
              margin: const EdgeInsets.only(right: 16),
              padding: const EdgeInsets.symmetric(horizontal: 12),
              decoration: BoxDecoration(
                  color: Colours.N1,
                  border:
                      Border(bottom: BorderSide(width: 1.0, color: Colours.B1)),
                  borderRadius: BorderRadius.circular(15)),
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    QsText(
                        logic.season.length > 4
                            ? ("${logic.season.substring(2, 4)}/${logic.season.substring(7, 9)}")
                            : logic.season,
                        needCenter: true,
                        fontSize: 12,
                        color: Colours.N9),
                    const SizedBox(width: 2),
                    Padding(
                        padding: const EdgeInsets.only(top: 1),
                        child: Image.asset(
                            width: 6, Utils.getImgPath('year_down_arrow.png')))
                  ]),
            ),
          ),
          Expanded(
              child: ChoiceBar(
                  initialIndex: logic.seasonIndex,
                  items: logic.typeList,
                  unselectColor: Colours.transparent,
                  onTap: (index) {
                    Utils.onEvent("sjpd_lx", params: {
                      "sjpd_lx": logic.umengList.indexOf(logic.typeList[index])
                    });
                    logic.pageController.jumpToPage(index);
                  })),
        ],
      ),
    );
  }
}
